package org.tekmob.remindpal.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Reminder CRUD Object
 * Method:
 * -createReminder	(CREATE)
 * -updateReminder	(UPDATE)
 * -deleteReminder	(DELETE)
 * -getAReminder	(RETRIEVE)
 * -getReminder		(RETRIEVE)
 * 
 * @author Afifa
 *
 */
public class ReminderDAO {

	private SQLiteDatabase db;
	private ReminderSQLiteHelper dbHelper;

	public ReminderDAO(Context context) {
		dbHelper = new ReminderSQLiteHelper(context);
		db = dbHelper.getWritableDatabase();

	}

	// Close the db
	public void close() {
		db.close();
	}

	/**
	 * Create new TODO object
	 * @param todoText
	 */
	public void createReminder(int ID,
			String NAME,
			int DATE,
			int MONTH,
			int YEAR,
			int HOUR,
			int MINUTE,
			String SOUND,
			String VIBRATE,
			String INCREASING,
			String MOVE,
			String SHAKE ) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("_id", ID);
		contentValues.put("name", NAME);
		contentValues.put("date" , DATE);
		contentValues.put("month", MONTH);
		contentValues.put("year", YEAR);
		contentValues.put("hour", HOUR);
		contentValues.put("minute", MINUTE);
		contentValues.put("sound", SOUND);
		contentValues.put("vibrate", VIBRATE);
		contentValues.put("increasing", INCREASING);
		contentValues.put("move", MOVE);
		contentValues.put("shake", SHAKE);
		// Insert into DB
		db.insert("reminders", null, contentValues);
	}
	
	

	/**
	 * Delete TODO object
	 * @param todoId
	 */
	public void deleteReminder(String name) {
		// Delete from DB where id match
		db.delete("reminders", "name = '" + name + "'", null);
	}
	
	/**
	 * Delete TODO object
	 * @param todoId
	 */
	public void updateReminder(int ID,
			String NAME,
			int DATE,
			int MONTH,
			int YEAR,
			int HOUR,
			int MINUTE,
			String SOUND,
			String VIBRATE,
			String INCREASING,
			String MOVE,
			String SHAKE ) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("_id", ID);
		contentValues.put("name", NAME);
		contentValues.put("date" , DATE);
		contentValues.put("month", MONTH);
		contentValues.put("year", YEAR);
		contentValues.put("hour", HOUR);
		contentValues.put("minute", MINUTE);
		contentValues.put("sound", SOUND);
		contentValues.put("vibrate", VIBRATE);
		contentValues.put("increasing", INCREASING);
		contentValues.put("move", MOVE);
		contentValues.put("shake", SHAKE);
		// Insert into DB
		//db.update("reminders", contentValues, "name = '"+NAME+"'", null);
		db.update("reminders", contentValues, "_id = " + ID, null);
	}
	

	/**
	 * Get all TODOs.
	 * @return
	 */
	public List<Reminder> getReminder() {
		db = dbHelper.getWritableDatabase();

		List<Reminder> reminderList = new ArrayList<Reminder>();

		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id", "name", "date", "month", "year",
				"hour", "minute", "sound", "vibrate", "increasing", "move", "shake"};

		// Query the database
		Cursor cursor = db.query("reminders", tableColumns, null, null, null, null, null);
		cursor.moveToFirst();

		// Iterate the results
		while (!cursor.isAfterLast()) {
			Reminder remind = new Reminder();
			// Take values from the DB
			remind.setId(cursor.getInt(0));
			remind.setName(cursor.getString(1));
			remind.setDate(cursor.getInt(2));
			remind.setMonth(cursor.getInt(3));
			remind.setYear(cursor.getInt(4));
			remind.setHour(cursor.getInt(5));
			remind.setMinute(cursor.getInt(6));
			remind.setSound(cursor.getString(7));
			remind.setVibrate(cursor.getString(8));
			remind.setIncreasing(cursor.getString(9));
			remind.setMove(cursor.getString(10));
			remind.setShake(cursor.getString(11));


			// Add to the DB
			reminderList.add(remind);

			// Move to the next result
			cursor.moveToNext();
		}
		//db.close();
		return reminderList;
	}

	public List<Reminder> getAReminder(int ID) {
		db = dbHelper.getWritableDatabase();
		//name = "lala";
		List<Reminder> reminderList = new ArrayList<Reminder>();
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id", "name", "date", "month", "year",
				"hour", "minute", "sound", "vibrate", "increasing", "move", "shake"};

		// Query the database
		//Cursor cursor = db.query("reminders", tableColumns, "name = '" +name+ "'", null, null, null, null);
		Cursor cursor = db.query("reminders", tableColumns, "_id = " + ID, null, null, null, null);
		// Take values from the DB
		while (cursor.moveToNext()) {
			Reminder remind = new Reminder();
			// Take values from the DB
			remind.setId(cursor.getInt(0));
			remind.setName(cursor.getString(1));
			remind.setDate(cursor.getInt(2));
			remind.setMonth(cursor.getInt(3));
			remind.setYear(cursor.getInt(4));
			remind.setHour(cursor.getInt(5));
			remind.setMinute(cursor.getInt(6));
			remind.setSound(cursor.getString(7));
			remind.setVibrate(cursor.getString(8));
			remind.setIncreasing(cursor.getString(9));
			remind.setMove(cursor.getString(10));
			remind.setShake(cursor.getString(11));
			// Add to the DB
			reminderList.add(remind);
		}

		//db.close();
		return reminderList;
	}

	public int getReminderId(String name) {
		db = dbHelper.getWritableDatabase();
		//name = "lala";
		int id = 0;
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id", "name", "date", "month", "year",
				"hour", "minute", "sound", "vibrate", "increasing", "move", "shake"};
		
		// Query the database
		Cursor cursor = db.query("reminders", tableColumns, "name = '" +name+ "'", null, null, null, null);
		//Cursor cursor = db.query("reminders", tableColumns, "_id = " + ID, null, null, null, null);
		// Take values from the DB
		while (cursor.moveToNext()) {
			id = cursor.getInt(0);
		}
		
		//db.close();
		return id;
	}

}
